﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Data;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.GenericMetadata;
using System.Windows.Forms;

namespace SdmxMl.Manager
{
    public class MetadataSetManager : ArtefactManager
    {
        public MetadataSetManager(SdmxArtefactType aType)
            : base(aType)
        {
            if (artefactType != SdmxArtefactType.MetadataSet)
                throw new ApplicationException("Unexpected. Invalid construction for MetadataSetManager");
        }

        public override bool LoadFromDocument(XmlDocument xDoc, SdmxNamespaceManager nsmgr)
        {
            bool found = false;
            XmlNodeList nodeList = xDoc.SelectNodes(string.Format("//genericmetadata:MetadataSet"), nsmgr);
            if (nodeList.Count > 0)
            {
                foreach (XmlNode nd in nodeList)
                {
              //      MetadataSetType a = new MetadataSetType(nd, nsmgr);
                    Artefact a = CreateArtefact(nd, nsmgr);
                    if (a != null && artefactList.Exist(a) == false)
                        artefactList.Add(a);
                }

                found = true;
            }
            return found;
        }

        public override void WriteXml(XmlTextWriter xw, ISdmxArtefactRef selectedArtefact)
        {
            if (artefactList.Count > 0)
            {
                string prefix = "genericmetadata";

                foreach (MetadataSetType m in artefactList)
                {
                    if (selectedArtefact == null || m == selectedArtefact)
                        m.WriteXml(xw, prefix, "MetadataSet");
                }
            }
        }

        public override TreeNode FillNode(TreeNode parentNode, int startIndex)
        {
            TreeNode createdNode = null;
            // already attached to parent, Bind List of artefacts
            for (int i = startIndex; i < artefactList.Count; i++)
                createdNode = artefactList[i].FillNode(parentNode);
            return createdNode;
        }
        
        public override XmlDocument GetAsDocument(ISdmxArtefactRef ia)
        {
            XmlDocument xDoc = new XmlDocument();
            MetadataSetType ar = ia as MetadataSetType;
            if (ar != null)
            {
                using (MemoryStream m = new MemoryStream(1024))
                {
                    using (XmlTextWriter xw = new XmlTextWriter(m, System.Text.Encoding.UTF8))
                    {
                        xw.Formatting = Formatting.Indented;
                        xw.WriteStartDocument();

                        SdmxMl.Msg.HeaderGenerator.GetHeader().WriteXml(xw, SdmxMl.Msg.HeaderType.ReferenceScheme.MetadataSet);
                        WriteXml(xw, ar);

                        xw.WriteEndDocument();
                        xw.Flush();

                        m.Flush();
                        m.Seek(0, SeekOrigin.Begin);
                        xDoc.Load(m);
                    }
                }
            }

            return xDoc;
        }
         
    }
}
